package org.nowcoder.leetcode.backTracking;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Title  : 77. Combinations
 * Source : https://leetcode.cn/problems/combinations/description/
 * Author : XrazYang
 * Date   : 2024-1-25
 */

public class LeetCode_77 {
    private List<List<Integer>> result = new ArrayList<>();
    private LinkedList<Integer> path = new LinkedList<>();

    private void backtracking(int n, int k, int start) {
        if (path.size() == k) {
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i = start; i <= n - (k - path.size()) + 1; i++) {
            path.add(i);
            backtracking(n, k, i + 1); // 递归
            path.removeLast(); ///回溯，撤销处理的节点
        }
    }

    public List<List<Integer>> combine(int n, int k) {
        backtracking(n, k, 1);
        return result;
    }
}
